package coreModel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import common.Result;

import dataPersistence.TransactionManager;
import dataPersistence.dao.IItemDAO;
import dataPersistence.dto.ItemDTO;


/**
 * RemovedItems is a container that stores all the items that were removed from the HIT.
 * @author The Fantastic 4
 *
 */
public class RemovedItemManager extends SerializableImplementer{

		private Date lastTimeReportRan;
        private Collection<Item> removedItems;
       //added for the expired item report
        private Map<Product,Collection<Item>> productToRemovedItems;
       
        /**
         * Constructs an empty manager
         */
        public RemovedItemManager()
        {
        	removedItems = new HashSet<Item>();
        	productToRemovedItems= new HashMap<Product, Collection<Item>>();
        }
        
        /**
         * Gets all the items in the manager
         * @return Item
         */
        public Collection<Item> getAllItems()
        {
        	return new HashSet<Item>(removedItems);
        }

        
        /**
         * adds the item to the container
         * @param item Item
         */
        public void addItem(Item item, boolean updateDatabase)
        {
        	removedItems.add(item);
        	if(productToRemovedItems.containsKey(item.getProduct()))
        	{
        		productToRemovedItems.get(item.getProduct()).add(item);
        	}
        	//add to map
        	else
        	{
        		ArrayList<Item> tmp = new ArrayList<Item>();
        		tmp.add(item);
        		productToRemovedItems.put(item.getProduct(),tmp );
        	}
        	
    		//data persistence
        	if (updateDatabase)
        	{
	    		Result result = new Result();
	    		if(!result.getStatus()) System.err.println(result.getMessage());
	    		if (!HIT.getInstance().isLoadingFromDataBase())
	    		{
	    			IItemDAO dao = HIT.getInstance().getFactory().getItemDAO();
	    			ItemDTO dto = new ItemDTO(item);
	    			result = dao.add(dto);
	    			if(!result.getStatus()) System.err.println(result.getMessage());
	    		}
        	}
        }
        
        /**
         * Removes an item from the container
         * @param item The item
         */
        public void removeItem(Item item)
        {
        	removedItems.remove(item);
        	if(productToRemovedItems.containsKey(item.getProduct()))
        		productToRemovedItems.get(item.getProduct()).remove(item);
        }
        
        public Set<Product> getAllProducts()
        {
        	return productToRemovedItems.keySet();
        }
        
        public  Collection<Item> getRemovedItemsByProduct(Product p)
        {
        	// if it is null or doesn't contain the key return
        	if(productToRemovedItems==null || !productToRemovedItems.containsKey(p))
        		return null;
        	return productToRemovedItems.get(p);
        }
        
        public boolean isThereALastTimeRan()
        {
        	if (lastTimeReportRan==null)
        	{
        		return false;
        	}
        	return true;
        }
        public Date getlastTimeRan()
        {
        	return lastTimeReportRan;
        }

		public void setLastReportDate(Date date)
		{
			lastTimeReportRan=date;
			
		}

}

